package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.CheckException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class DishServiceImpl implements DishService {
    private final DishMapper dishMapper;
    private final DishFlavorMapper dishFlavorMapper;

    /**
     * 新增菜品
     * @param dishDTO the dish dto
     */
    @Override
    @Transactional
    public void create(DishDTO dishDTO) {
        // 参数校验
        String name = dishDTO.getName();
        if (Objects.isNull(name) || name.length()<2 || name.length() >20){
            throw new CheckException(MessageConstant.DISH_NAME_ERROR);
        }
        Dish dishQuery = new Dish();
        dishQuery.setName(dishDTO.getName());
        Integer count= dishMapper.selectCount(dishQuery);
        if (count>0){
            throw new CheckException(MessageConstant.DISH_NAME_EXIST);
        }
        // 菜品数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);

        // 多个口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (Objects.nonNull(flavors) && !flavors.isEmpty()){
            flavors.forEach(item -> item.setDishId(dish.getId()));
            // 插入多个口味数据
            dishFlavorMapper.insertBatch(flavors);
        }



    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO the dish page query dto
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        // 参数校验
        int page = Objects.isNull(dishPageQueryDTO.getPage()) ? 1 : dishPageQueryDTO.getPage();
        int pageSize = Objects.isNull(dishPageQueryDTO.getPageSize()) ? 10 : dishPageQueryDTO.getPageSize();
        PageHelper.startPage(page,pageSize);
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishPageQueryDTO,dish);

        Page<Dish> dishPage = dishMapper.selectList(dish);

        long total = dishPage.getTotal();
        List<Dish> result = dishPage.getResult();
        return new PageResult(total,result);
    }

    /**
     * 批量删除菜品
     * @param ids the ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        Integer count = dishMapper.selectCountByIdsAndStatus(ids, StatusConstant.ENABLE);
        if (count > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        // 批量删除菜品
        dishMapper.deleteById(ids);

        // 批量删除口味数据
        dishFlavorMapper.deleteByDishId(ids);

    }

    /**
     * 菜品查询回显
     * @param id the id
     * @return
     */
    @Override
    public DishVO infoById(Long id) {
        // 查询菜品数据
        Dish dish = dishMapper.selectById(id);
        // 查询口味数据
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectByDishId(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    /**
     * 修改菜品
     * @param dishDTO the dish dto
     */
    @Override
    @Transactional
    public void update(DishDTO dishDTO) {
        // 参数校验
        String name = dishDTO.getName();
        if (Objects.isNull(name) || name.length()<2 || name.length() >20){
            throw new CheckException(MessageConstant.DISH_NAME_ERROR);
        }
        Dish dishQuery = new Dish();
        dishQuery.setName(dishDTO.getName());
        dishQuery.setId(dishDTO.getId());
        Integer count= dishMapper.selectCount(dishQuery);
        if (count>0){
            throw new CheckException(MessageConstant.DISH_NAME_EXIST);
        }
        // 修改菜品数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updateById(dish);
        // 修改口味数据 先全部删除再新增口味
        dishFlavorMapper.deleteByDishId(Collections.singletonList(dish.getId()));
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (Objects.nonNull(flavors) && !flavors.isEmpty()){
            flavors.forEach(item -> item.setDishId(dish.getId()));
            // 插入多个口味数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }
}
